﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Objects;

namespace SodiumHydroxide
{
    public partial class PayrollView : Form
    {
        PublishingCompanyEntities publishContext;
        Payroll currentPayroll;


        public PayrollView()
        {
            InitializeComponent();
        }

        private void PayrollView_Load(object sender, EventArgs e)
        {
            publishContext = new PublishingCompanyEntities();

            ////This only displays the first name.
            authorList.DataSource = publishContext.Author;
            authorList.DisplayMember = "FirstName";


            //Uses "ADO.NET Entity SQL Expressions" or "method based queries"
            //authorList.DataSource = publishContext.Author.Select("it.FirstName + ' ' + it.LastName As FullName, it.AuthorID");
            //authorList.DisplayMember = "FullName";
            //authorList.ValueMember = "AuthorID";
            

        }

        private void authorList_SelectedIndexChanged(object sender, EventArgs e)
        {
            Author selectedAuthor = (Author)authorList.SelectedItem;
            int selectedAuthorID = selectedAuthor.AuthorID;

            //Uses Linq-to-Entities
            IQueryable<Payroll> payrollQuery = from p in publishContext.Payroll
                                                where p.Author.AuthorID == selectedAuthorID
                                                select p;
            List<Payroll> selectedPayroll = payrollQuery.ToList();

            if (selectedPayroll != null && selectedPayroll.Count > 0)
            {
                currentPayroll = selectedPayroll.First();
            }
            else
            {
                currentPayroll = null;
            }

            PopulateFields();

        }

        private void PayrollView_FormClosing(object sender, FormClosingEventArgs e)
        {
            publishContext.Dispose();
        }

        private void updateButton_Click(object sender, EventArgs e)
        {
            currentPayroll.Salary = Convert.ToInt16(salaryUpDown.Value);
            int rowsAffected = publishContext.SaveChanges();
            MessageBox.Show(rowsAffected.ToString() + " changes made to the table");

        }

        private void deleteButton_Click(object sender, EventArgs e)
        {
            publishContext.DeleteObject(currentPayroll);
            publishContext.SaveChanges(true);
            currentPayroll = null;
            PopulateFields();

        }

        private void addButton_Click(object sender, EventArgs e)
        {
            Payroll newPayroll = new Payroll();
            Author selectedAuthor = (Author)authorList.SelectedItem;
            newPayroll.Author = selectedAuthor;
            newPayroll.Salary = Convert.ToInt16(salaryUpDown.Value);

            publishContext.AddToPayroll(newPayroll);
            publishContext.SaveChanges();
            currentPayroll = newPayroll; //newPayroll.PayrollID now matches the database table!

            PopulateFields();

        }

        private void PopulateFields()
        {
            if (currentPayroll != null)
            {
                payrollIDLabel.Text = currentPayroll.PayrollID.ToString();
                salaryUpDown.Value = (decimal)currentPayroll.Salary;
                addButton.Enabled = false;
                deleteButton.Enabled = true;
                updateButton.Enabled = true;
            }
            else
            {
                payrollIDLabel.Text = "Not on payroll";
                salaryUpDown.Value = 0;
                addButton.Enabled = true;
                deleteButton.Enabled = false;
                updateButton.Enabled = false;
            }
        }

    }
}
